<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: Playground.class.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

class Playground extends JLClass{
  
  var $id;
  var $name;
  var $short_name;
  var $address;
  var $zip;
  var $city;
  var $country;
  var $max_visitors;
  var $picture;
  var $description;
  var $club_id;
  var $link;
  var $checked_out;
  var $checked_out_time;
  
  function Playground( & $db){
    $this->mosDBTable('#__joomleague_playgrounds','id',$db);
  }
  
  /**
   * returns upcoming published games for this playground
   * @param project int project id, if 0 scope is all projects 
   * @return array games info
   */           
  function getNextGames( $project = 0 ) {
    $query = "SELECT m.*, p.name AS project_name 
                  FROM #__joomleague_matches AS m
                  INNER JOIN #__joomleague AS p ON p.id=m.project_id
                  INNER JOIN #__joomleague_team_joomleague tj ON tj.team_id = m.matchpart1
                  INNER JOIN #__joomleague_teams t ON t.id = m.matchpart1
                  INNER JOIN #__joomleague_clubs c ON c.id = t.club_id
                  WHERE (m.playground_id=$this->id OR (tj.standard_playground = $this->id AND m.playground_id IS NULL)
                  OR (c.standard_playground = $this->id AND m.playground_id IS NULL))
                  AND m.match_date > NOW()
                  AND m.published = 1
                  AND p.published = 1";
    if ($project) {
      $query .= " AND project_id=$project";  
    }
    $query .= " GROUP BY m.match_id ORDER BY match_date ASC";               
    $this->_db->setQuery($query);
    if (! $result = $this->_db->loadObjectList() ){
      echo "<pre>".$this->_db->getErrorMsg()."</pre";         
    }
    return $result;
  }
}

?>
